package com.leetcode.dfs;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Leetcode90 {
    List<List<Integer>> res = new ArrayList<>();
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        Arrays.sort(nums);
        LinkedList<Integer> path = new LinkedList<>();
        // res.add(new LinkedList<>(path));
        dfs(nums, 0, path);
        return res;
    }

    private void dfs(int[] nums, int index, LinkedList<Integer> path) {
        if (index > nums.length) {
            return;
        }
        if (!res.contains(new LinkedList<>(path))) {
            res.add(new LinkedList<>(path));
        }
        for (int i = index; i < nums.length; i++) {
            path.add(nums[i]);
            dfs(nums, i + 1, path);
            path.removeLast();
        }
    }

    public static void main(String[] args) {
        Leetcode90 leetcode90 = new Leetcode90();
        List<List<Integer>> ans = leetcode90.subsetsWithDup(new int[]{4, 4, 4, 1, 4});
        System.out.println(ans);
    }

}
